package day021.homework.p7;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;

public class UploadThread implements Runnable {
    private Socket socket;

    public UploadThread() {
    }

    public UploadThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            File dir = new File("D:\\java\\uploads");
            dir.mkdirs();

            String fileName = "come.itheima" + System.currentTimeMillis() + ".png";
            FileOutputStream fos = new FileOutputStream(new File(dir, fileName));

            InputStream in = socket.getInputStream();
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = in.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }

            socket.getOutputStream().write("上传成功！".getBytes());

            socket.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
